/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helloscala.practise.chapter4

import helloscala.practise.ChapterEntry

object Chapter4Entry extends ChapterEntry {
  def runTests: Unit = {
    testTraitConstructor
  }

  //like classes, the body of a trait is executed every time an instance is created that uses the trait
  def testTraitConstructor: Unit = {
    trait T1 {
      println( "  in T1: x = " + x )
      val x=1
      println( "  in T1: x = " + x )
    }
    trait T2 {
      println( "  in T2: y = " + y )
      val y="T2"
      println( "  in T2: y = " + y )
    }

    class Base12 {
      println( "  in Base12: b = " + b )
      val b="Base12"
      println( "  in Base12: b = " + b )
    }
    class C12 extends Base12 with T1 with T2 {
      println( "  in C12: c = " + c )
      val c="C12"
      println( "  in C12: c = " + c )
    }
    println( "Creating C12:" )
    new C12
    println( "After Creating C12" )
  }

  def testMultiObserveClicks: Unit = {
    class Button(label: String) extends Widget with Clickable {
      def click = {
        //do something non-related to callback process
      }
    }

    //mixin of trait don't have to be static, it may happen when object instantiated
    val targetButton = new Button("OK") with ObservableClicks with VetoableClicks
//    val targetButton = new Button("OK") with VetoableClicks with ObservableClicks //order of trait results in diff result
    val observer = new ButtonCountObserver
    targetButton.addObserver(observer)

    for (i <- 1 to 5) targetButton.click
    println("Was clicked for [" + observer.count + "] times")
  }

  def testObserveClicks: Unit = {
    class Button(label: String) extends Widget with Clickable {
      def click = {
        //do something non-related to callback process
      }
    }

    //mixin of trait don't have to be static, it may happen when object instantiated
    val targetButton = new Button("OK") with ObservableClicks
    val observer = new ButtonCountObserver
    targetButton.addObserver(observer)

    for (i <- 1 to 4) targetButton.click
    println("Was clicked for [" + observer.count + "] times")
  }

  def testCounterObserver: Unit = {
    val targetButton = new ObservableButton("OK")
    val observer = new ButtonCountObserver
    targetButton.addObserver(observer)

    for (i <- 1 to 3) targetButton.click
    println("Was clicked for [" + observer.count + "] times")
  }
}
